﻿#region Copyright (c) 2012-11, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Reflection;


namespace Amarok.Agents
{
	/// <summary>
	/// This attribute specifies the parallelism level of a message handler.
	/// </summary>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
	public abstract class ParallelismLevelAttribute : Attribute
	{
		#region ++ Public Static Interface ++

		/// <summary>
		/// Returns the parallelism level that is defined for the given method, or the specified default value, 
		/// if no parallelism level is defined.
		/// </summary>
		/// 
		/// <param name="methodInfo">
		/// The method for which the parallelism level should be returned.</param>
		/// <param name="defaultValue">
		/// The default parallelism level that should be returned, if no parallelism level is defined for the 
		/// given method.</param>
		/// 
		/// <returns>
		/// The parallelism level for the given method, if defined; otherwise the specified default value.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static Int32 GetParallelismLevel(MethodInfo methodInfo, Int32 defaultValue)
		{
			Verify.NotNull(methodInfo, "methodInfo");

			var attribute = (ParallelismLevelAttribute)Attribute.GetCustomAttribute(
				methodInfo,
				typeof(ParallelismLevelAttribute),
				true);

			if (attribute == null)
				return defaultValue;

			return attribute.ParallelismLevel;
		}

		#endregion

		#region ++ Public Interface ++

		/// <summary>
		/// Gets the parallelism level defined for the message handler.
		/// </summary>
		public Int32 ParallelismLevel
		{
			get;
			private set;
		}

		#endregion

		#region ## Protected Interface ##

		/// <summary>
		/// Initializes a new instance.
		/// </summary>
		/// 
		/// <param name="parallelismLevel">
		/// The parallelism level for the message handler.</param>
		protected ParallelismLevelAttribute(Int32 parallelismLevel)
		{
			if (parallelismLevel == 0)
				this.ParallelismLevel = Amarok.Agents.ParallelismLevel.NoParallelism;
			else if (parallelismLevel > 0)
				this.ParallelismLevel = parallelismLevel;
			else
				this.ParallelismLevel = Amarok.Agents.ParallelismLevel.MaximumParallelism;
		}

		#endregion

	}
}
